home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / wstools / XMLSchema.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  76.8 KB  |  2,740 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. ident = '$Id: XMLSchema.py 1434 2007-11-01 22:42:47Z boverhof $'
  5. import types
  6. import weakref
  7. import sys
  8. import warnings
  9. from Namespaces import SCHEMA, XMLNS, SOAP
  10. from Utility import DOM, DOMException, Collection, SplitQName, basejoin
  11. from StringIO import StringIO
  12.  
  13. try:
  14.     from threading import RLock
  15. except ImportError:
  16.     
  17.     class RLock:
  18.         
  19.         def acquire():
  20.             pass
  21.  
  22.         
  23.         def release():
  24.             pass
  25.  
  26.  
  27.  
  28. TYPES = 'types'
  29. ATTRIBUTE_GROUPS = 'attr_groups'
  30. ATTRIBUTES = 'attr_decl'
  31. ELEMENTS = 'elements'
  32. MODEL_GROUPS = 'model_groups'
  33. BUILT_IN_NAMESPACES = [
  34.     SOAP.ENC] + SCHEMA.XSD_LIST
  35.  
  36. def GetSchema(component):
  37.     parent = component
  38.     while not isinstance(parent, XMLSchema):
  39.         parent = parent._parent()
  40.     return parent
  41.  
  42.  
  43. class SchemaReader:
  44.     namespaceToSchema = { }
  45.     
  46.     def __init__(self, domReader = None, base_url = None):
  47.         self._SchemaReader__base_url = base_url
  48.         self._SchemaReader__readerClass = domReader
  49.         if not self._SchemaReader__readerClass:
  50.             self._SchemaReader__readerClass = DOMAdapter
  51.         
  52.         self._includes = { }
  53.         self._imports = { }
  54.  
  55.     
  56.     def __setImports(self, schema):
  57.         for ns, val in schema.imports.items():
  58.             if self._imports.has_key(ns):
  59.                 schema.addImportSchema(self._imports[ns])
  60.                 continue
  61.         
  62.  
  63.     
  64.     def __setIncludes(self, schema):
  65.         for schemaLocation, val in schema.includes.items():
  66.             if self._includes.has_key(schemaLocation):
  67.                 schema.addIncludeSchema(schemaLocation, self._imports[schemaLocation])
  68.                 continue
  69.         
  70.  
  71.     
  72.     def addSchemaByLocation(self, location, schema):
  73.         self._includes[location] = schema
  74.  
  75.     
  76.     def addSchemaByNamespace(self, schema):
  77.         self._imports[schema.targetNamespace] = schema
  78.  
  79.     
  80.     def loadFromNode(self, parent, element):
  81.         reader = self._SchemaReader__readerClass(element)
  82.         schema = XMLSchema(parent)
  83.         schema.wsdl = parent
  84.         schema.setBaseUrl(self._SchemaReader__base_url)
  85.         schema.load(reader)
  86.         return schema
  87.  
  88.     
  89.     def loadFromStream(self, file, url = None):
  90.         reader = self._SchemaReader__readerClass()
  91.         reader.loadDocument(file)
  92.         schema = XMLSchema()
  93.         if url is not None:
  94.             schema.setBaseUrl(url)
  95.         
  96.         schema.load(reader)
  97.         self._SchemaReader__setIncludes(schema)
  98.         self._SchemaReader__setImports(schema)
  99.         return schema
  100.  
  101.     
  102.     def loadFromString(self, data):
  103.         return self.loadFromStream(StringIO(data))
  104.  
  105.     
  106.     def loadFromURL(self, url, schema = None):
  107.         reader = self._SchemaReader__readerClass()
  108.         if self._SchemaReader__base_url:
  109.             url = basejoin(self._SchemaReader__base_url, url)
  110.         
  111.         reader.loadFromURL(url)
  112.         if not schema:
  113.             pass
  114.         schema = XMLSchema()
  115.         schema.setBaseUrl(url)
  116.         schema.load(reader)
  117.         self._SchemaReader__setIncludes(schema)
  118.         self._SchemaReader__setImports(schema)
  119.         return schema
  120.  
  121.     
  122.     def loadFromFile(self, filename):
  123.         if self._SchemaReader__base_url:
  124.             filename = basejoin(self._SchemaReader__base_url, filename)
  125.         
  126.         file = open(filename, 'rb')
  127.         
  128.         try:
  129.             schema = self.loadFromStream(file, filename)
  130.         finally:
  131.             file.close()
  132.  
  133.         return schema
  134.  
  135.  
  136.  
  137. class SchemaError(Exception):
  138.     pass
  139.  
  140.  
  141. class NoSchemaLocationWarning(Exception):
  142.     pass
  143.  
  144.  
  145. class DOMAdapterInterface:
  146.     
  147.     def hasattr(self, attr, ns = None):
  148.         raise NotImplementedError, 'adapter method not implemented'
  149.  
  150.     
  151.     def getContentList(self, *contents):
  152.         raise NotImplementedError, 'adapter method not implemented'
  153.  
  154.     
  155.     def setAttributeDictionary(self, attributes):
  156.         raise NotImplementedError, 'adapter method not implemented'
  157.  
  158.     
  159.     def getAttributeDictionary(self):
  160.         raise NotImplementedError, 'adapter method not implemented'
  161.  
  162.     
  163.     def getNamespace(self, prefix):
  164.         raise NotImplementedError, 'adapter method not implemented'
  165.  
  166.     
  167.     def getTagName(self):
  168.         raise NotImplementedError, 'adapter method not implemented'
  169.  
  170.     
  171.     def getParentNode(self):
  172.         raise NotImplementedError, 'adapter method not implemented'
  173.  
  174.     
  175.     def loadDocument(self, file):
  176.         raise NotImplementedError, 'adapter method not implemented'
  177.  
  178.     
  179.     def loadFromURL(self, url):
  180.         raise NotImplementedError, 'adapter method not implemented'
  181.  
  182.  
  183.  
  184. class DOMAdapter(DOMAdapterInterface):
  185.     
  186.     def __init__(self, node = None):
  187.         if hasattr(node, 'documentElement'):
  188.             self._DOMAdapter__node = node.documentElement
  189.         else:
  190.             self._DOMAdapter__node = node
  191.         self._DOMAdapter__attributes = None
  192.  
  193.     
  194.     def getNode(self):
  195.         return self._DOMAdapter__node
  196.  
  197.     
  198.     def hasattr(self, attr, ns = None):
  199.         if not self._DOMAdapter__attributes:
  200.             self.setAttributeDictionary()
  201.         
  202.         if ns:
  203.             return self._DOMAdapter__attributes.get(ns, { }).has_key(attr)
  204.         
  205.         return self._DOMAdapter__attributes.has_key(attr)
  206.  
  207.     
  208.     def getContentList(self, *contents):
  209.         nodes = []
  210.         ELEMENT_NODE = self._DOMAdapter__node.ELEMENT_NODE
  211.         for child in DOM.getElements(self._DOMAdapter__node, None):
  212.             if child.nodeType == ELEMENT_NODE and SplitQName(child.tagName)[1] in contents:
  213.                 nodes.append(child)
  214.                 continue
  215.         
  216.         return map(self.__class__, nodes)
  217.  
  218.     
  219.     def setAttributeDictionary(self):
  220.         self._DOMAdapter__attributes = { }
  221.         for v in self._DOMAdapter__node._attrs.values():
  222.             self._DOMAdapter__attributes[v.nodeName] = v.nodeValue
  223.         
  224.  
  225.     
  226.     def getAttributeDictionary(self):
  227.         if not self._DOMAdapter__attributes:
  228.             self.setAttributeDictionary()
  229.         
  230.         return self._DOMAdapter__attributes
  231.  
  232.     
  233.     def getTagName(self):
  234.         return self._DOMAdapter__node.tagName
  235.  
  236.     
  237.     def getParentNode(self):
  238.         if self._DOMAdapter__node.parentNode.nodeType == self._DOMAdapter__node.ELEMENT_NODE:
  239.             return DOMAdapter(self._DOMAdapter__node.parentNode)
  240.         
  241.  
  242.     
  243.     def getNamespace(self, prefix):
  244.         namespace = None
  245.         if prefix == 'xmlns':
  246.             namespace = DOM.findDefaultNS(prefix, self._DOMAdapter__node)
  247.         else:
  248.             
  249.             try:
  250.                 namespace = DOM.findNamespaceURI(prefix, self._DOMAdapter__node)
  251.             except DOMException:
  252.                 ex = None
  253.                 if prefix != 'xml':
  254.                     raise SchemaError, '%s namespace not declared for %s' % (prefix, self._DOMAdapter__node._get_tagName())
  255.                 
  256.                 namespace = XMLNS.XML
  257.  
  258.         return namespace
  259.  
  260.     
  261.     def loadDocument(self, file):
  262.         self._DOMAdapter__node = DOM.loadDocument(file)
  263.         if hasattr(self._DOMAdapter__node, 'documentElement'):
  264.             self._DOMAdapter__node = self._DOMAdapter__node.documentElement
  265.         
  266.  
  267.     
  268.     def loadFromURL(self, url):
  269.         self._DOMAdapter__node = DOM.loadFromURL(url)
  270.         if hasattr(self._DOMAdapter__node, 'documentElement'):
  271.             self._DOMAdapter__node = self._DOMAdapter__node.documentElement
  272.         
  273.  
  274.  
  275.  
  276. class XMLBase:
  277.     tag = None
  278.     __indent = 0
  279.     __rlock = RLock()
  280.     
  281.     def __str__(self):
  282.         XMLBase._XMLBase__rlock.acquire()
  283.         XMLBase._XMLBase__indent += 1
  284.         tmp = '<' + str(self.__class__) + '>\n'
  285.         for k, v in self.__dict__.items():
  286.             tmp += '%s* %s = %s\n' % (XMLBase._XMLBase__indent * '  ', k, v)
  287.         
  288.         XMLBase._XMLBase__indent -= 1
  289.         XMLBase._XMLBase__rlock.release()
  290.         return tmp
  291.  
  292.  
  293.  
  294. class DefinitionMarker:
  295.     pass
  296.  
  297.  
  298. class DeclarationMarker:
  299.     pass
  300.  
  301.  
  302. class AttributeMarker:
  303.     pass
  304.  
  305.  
  306. class AttributeGroupMarker:
  307.     pass
  308.  
  309.  
  310. class WildCardMarker:
  311.     pass
  312.  
  313.  
  314. class ElementMarker:
  315.     pass
  316.  
  317.  
  318. class ReferenceMarker:
  319.     pass
  320.  
  321.  
  322. class ModelGroupMarker:
  323.     pass
  324.  
  325.  
  326. class AllMarker(ModelGroupMarker):
  327.     pass
  328.  
  329.  
  330. class ChoiceMarker(ModelGroupMarker):
  331.     pass
  332.  
  333.  
  334. class SequenceMarker(ModelGroupMarker):
  335.     pass
  336.  
  337.  
  338. class ExtensionMarker:
  339.     pass
  340.  
  341.  
  342. class RestrictionMarker:
  343.     facets = [
  344.         'enumeration',
  345.         'length',
  346.         'maxExclusive',
  347.         'maxInclusive',
  348.         'maxLength',
  349.         'minExclusive',
  350.         'minInclusive',
  351.         'minLength',
  352.         'pattern',
  353.         'fractionDigits',
  354.         'totalDigits',
  355.         'whiteSpace']
  356.  
  357.  
  358. class SimpleMarker:
  359.     pass
  360.  
  361.  
  362. class ListMarker:
  363.     pass
  364.  
  365.  
  366. class UnionMarker:
  367.     pass
  368.  
  369.  
  370. class ComplexMarker:
  371.     pass
  372.  
  373.  
  374. class LocalMarker:
  375.     pass
  376.  
  377.  
  378. class MarkerInterface:
  379.     
  380.     def isDefinition(self):
  381.         return isinstance(self, DefinitionMarker)
  382.  
  383.     
  384.     def isDeclaration(self):
  385.         return isinstance(self, DeclarationMarker)
  386.  
  387.     
  388.     def isAttribute(self):
  389.         return isinstance(self, AttributeMarker)
  390.  
  391.     
  392.     def isAttributeGroup(self):
  393.         return isinstance(self, AttributeGroupMarker)
  394.  
  395.     
  396.     def isElement(self):
  397.         return isinstance(self, ElementMarker)
  398.  
  399.     
  400.     def isReference(self):
  401.         return isinstance(self, ReferenceMarker)
  402.  
  403.     
  404.     def isWildCard(self):
  405.         return isinstance(self, WildCardMarker)
  406.  
  407.     
  408.     def isModelGroup(self):
  409.         return isinstance(self, ModelGroupMarker)
  410.  
  411.     
  412.     def isAll(self):
  413.         return isinstance(self, AllMarker)
  414.  
  415.     
  416.     def isChoice(self):
  417.         return isinstance(self, ChoiceMarker)
  418.  
  419.     
  420.     def isSequence(self):
  421.         return isinstance(self, SequenceMarker)
  422.  
  423.     
  424.     def isExtension(self):
  425.         return isinstance(self, ExtensionMarker)
  426.  
  427.     
  428.     def isRestriction(self):
  429.         return isinstance(self, RestrictionMarker)
  430.  
  431.     
  432.     def isSimple(self):
  433.         return isinstance(self, SimpleMarker)
  434.  
  435.     
  436.     def isComplex(self):
  437.         return isinstance(self, ComplexMarker)
  438.  
  439.     
  440.     def isLocal(self):
  441.         return isinstance(self, LocalMarker)
  442.  
  443.     
  444.     def isList(self):
  445.         return isinstance(self, ListMarker)
  446.  
  447.     
  448.     def isUnion(self):
  449.         return isinstance(self, UnionMarker)
  450.  
  451.  
  452.  
  453. class XMLSchemaComponent(XMLBase, MarkerInterface):
  454.     required = []
  455.     attributes = { }
  456.     contents = { }
  457.     xmlns_key = ''
  458.     xmlns = 'xmlns'
  459.     xml = 'xml'
  460.     
  461.     def __init__(self, parent = None):
  462.         self.attributes = None
  463.         self._parent = parent
  464.         if self._parent:
  465.             self._parent = weakref.ref(parent)
  466.         
  467.         if not (self.__class__ == XMLSchemaComponent):
  468.             if type(self.__class__.required) == type(XMLSchemaComponent.required) and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes):
  469.                 pass
  470.             if not (type(self.__class__.contents) == type(XMLSchemaComponent.contents)):
  471.                 raise RuntimeError, 'Bad type for a class variable in %s' % self.__class__
  472.             
  473.  
  474.     
  475.     def getItemTrace(self):
  476.         (item, path, name, ref) = (self, [], 'name', 'ref')
  477.         while not isinstance(item, XMLSchema) and not isinstance(item, WSDLToolsAdapter):
  478.             attr = item.getAttribute(name)
  479.             if not attr:
  480.                 attr = item.getAttribute(ref)
  481.                 if not attr:
  482.                     path.append('<%s>' % item.tag)
  483.                 else:
  484.                     path.append('<%s ref="%s">' % (item.tag, attr))
  485.             else:
  486.                 path.append('<%s name="%s">' % (item.tag, attr))
  487.             item = item._parent()
  488.         
  489.         try:
  490.             tns = item.getTargetNamespace()
  491.         except:
  492.             tns = ''
  493.  
  494.         path.append('<%s targetNamespace="%s">' % (item.tag, tns))
  495.         path.reverse()
  496.         return ''.join(path)
  497.  
  498.     
  499.     def getTargetNamespace(self):
  500.         parent = self
  501.         targetNamespace = 'targetNamespace'
  502.         tns = self.attributes.get(targetNamespace)
  503.         while not tns and parent and parent._parent is not None:
  504.             parent = parent._parent()
  505.             tns = parent.attributes.get(targetNamespace)
  506.         if not tns:
  507.             pass
  508.         return ''
  509.  
  510.     
  511.     def getAttributeDeclaration(self, attribute):
  512.         return self.getQNameAttribute(ATTRIBUTES, attribute)
  513.  
  514.     
  515.     def getAttributeGroup(self, attribute):
  516.         return self.getQNameAttribute(ATTRIBUTE_GROUPS, attribute)
  517.  
  518.     
  519.     def getTypeDefinition(self, attribute):
  520.         return self.getQNameAttribute(TYPES, attribute)
  521.  
  522.     
  523.     def getElementDeclaration(self, attribute):
  524.         return self.getQNameAttribute(ELEMENTS, attribute)
  525.  
  526.     
  527.     def getModelGroup(self, attribute):
  528.         return self.getQNameAttribute(MODEL_GROUPS, attribute)
  529.  
  530.     
  531.     def getQNameAttribute(self, collection, attribute):
  532.         tdc = self.getAttributeQName(attribute)
  533.         if not tdc:
  534.             return None
  535.         
  536.         obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName())
  537.         if obj:
  538.             return obj
  539.         
  540.  
  541.     
  542.     def getSchemaItem(self, collection, namespace, name):
  543.         parent = GetSchema(self)
  544.         if parent.targetNamespace == namespace:
  545.             
  546.             try:
  547.                 obj = getattr(parent, collection)[name]
  548.             except KeyError:
  549.                 ex = None
  550.                 raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
  551.  
  552.             return obj
  553.         
  554.         if not parent.imports.has_key(namespace):
  555.             if namespace in BUILT_IN_NAMESPACES:
  556.                 return None
  557.             
  558.             raise SchemaError, 'schema "%s" does not import namespace "%s"' % (parent.targetNamespace, namespace)
  559.         
  560.         schema = parent.imports[namespace]
  561.         if not isinstance(schema, XMLSchema):
  562.             schema = schema.getSchema()
  563.             if schema is not None:
  564.                 parent.imports[namespace] = schema
  565.             
  566.         
  567.         if schema is None:
  568.             if namespace in BUILT_IN_NAMESPACES:
  569.                 return None
  570.             
  571.             raise SchemaError, 'no schema instance for imported namespace (%s).' % namespace
  572.         
  573.         if not isinstance(schema, XMLSchema):
  574.             raise TypeError, 'expecting XMLSchema instance not "%r"' % schema
  575.         
  576.         
  577.         try:
  578.             obj = getattr(schema, collection)[name]
  579.         except KeyError:
  580.             ex = None
  581.             raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
  582.  
  583.         return obj
  584.  
  585.     
  586.     def getXMLNS(self, prefix = None):
  587.         if prefix == XMLSchemaComponent.xml:
  588.             return XMLNS.XML
  589.         
  590.         parent = self
  591.         if not prefix:
  592.             pass
  593.         ns = self.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
  594.         while not ns:
  595.             parent = parent._parent()
  596.             if not prefix:
  597.                 pass
  598.             ns = parent.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
  599.             if not ns and isinstance(parent, WSDLToolsAdapter):
  600.                 if prefix is None:
  601.                     return ''
  602.                 
  603.                 raise SchemaError, 'unknown prefix %s' % prefix
  604.                 continue
  605.         return ns
  606.  
  607.     
  608.     def getAttribute(self, attribute):
  609.         if type(attribute) in (list, tuple):
  610.             if len(attribute) != 2:
  611.                 raise LookupError, 'To access attributes must use name or (namespace,name)'
  612.             
  613.             ns_dict = self.attributes.get(attribute[0])
  614.             if ns_dict is None:
  615.                 return None
  616.             
  617.             return ns_dict.get(attribute[1])
  618.         
  619.         return self.attributes.get(attribute)
  620.  
  621.     
  622.     def getAttributeQName(self, attribute):
  623.         qname = self.getAttribute(attribute)
  624.         if isinstance(qname, TypeDescriptionComponent) is True:
  625.             return qname
  626.         
  627.         if qname is None:
  628.             return None
  629.         
  630.         (prefix, ncname) = SplitQName(qname)
  631.         namespace = self.getXMLNS(prefix)
  632.         return TypeDescriptionComponent((namespace, ncname))
  633.  
  634.     
  635.     def getAttributeName(self):
  636.         return self.getAttribute('name')
  637.  
  638.     
  639.     def setAttributes(self, node):
  640.         self.attributes = {
  641.             XMLSchemaComponent.xmlns: { } }
  642.         for k, v in node.getAttributeDictionary().items():
  643.             (prefix, value) = SplitQName(k)
  644.             if value == XMLSchemaComponent.xmlns:
  645.                 if not prefix:
  646.                     pass
  647.                 self.attributes[value][XMLSchemaComponent.xmlns_key] = v
  648.                 continue
  649.             if prefix:
  650.                 ns = node.getNamespace(prefix)
  651.                 if not ns:
  652.                     raise SchemaError, 'no namespace for attribute prefix %s' % prefix
  653.                 
  654.                 if not self.attributes.has_key(ns):
  655.                     self.attributes[ns] = { }
  656.                 elif self.attributes[ns].has_key(value):
  657.                     raise SchemaError, 'attribute %s declared multiple times in %s' % (value, ns)
  658.                 
  659.                 self.attributes[ns][value] = v
  660.                 continue
  661.             if not self.attributes.has_key(value):
  662.                 self.attributes[value] = v
  663.                 continue
  664.             raise SchemaError, 'attribute %s declared multiple times' % value
  665.         
  666.         if not isinstance(self, WSDLToolsAdapter):
  667.             self._XMLSchemaComponent__checkAttributes()
  668.         
  669.         self._XMLSchemaComponent__setAttributeDefaults()
  670.         for k in [
  671.             'type',
  672.             'element',
  673.             'base',
  674.             'ref',
  675.             'substitutionGroup',
  676.             'itemType']:
  677.             if self.attributes.has_key(k):
  678.                 (prefix, value) = SplitQName(self.attributes.get(k))
  679.                 self.attributes[k] = TypeDescriptionComponent((self.getXMLNS(prefix), value))
  680.                 continue
  681.         
  682.         for k in [
  683.             'memberTypes']:
  684.             if self.attributes.has_key(k):
  685.                 qnames = self.attributes[k]
  686.                 self.attributes[k] = []
  687.                 for qname in qnames.split():
  688.                     (prefix, value) = SplitQName(qname)
  689.                     self.attributes['memberTypes'].append(TypeDescriptionComponent((self.getXMLNS(prefix), value)))
  690.                 
  691.         
  692.  
  693.     
  694.     def getContents(self, node):
  695.         return node.getContentList(*self.__class__.contents['xsd'])
  696.  
  697.     
  698.     def __setAttributeDefaults(self):
  699.         for k, v in self.__class__.attributes.items():
  700.             if v is not None and self.attributes.has_key(k) is False:
  701.                 if isinstance(v, types.FunctionType):
  702.                     self.attributes[k] = v(self)
  703.                 else:
  704.                     self.attributes[k] = v
  705.             isinstance(v, types.FunctionType)
  706.         
  707.  
  708.     
  709.     def __checkAttributes(self):
  710.         for a in self.__class__.required:
  711.             if not self.attributes.has_key(a):
  712.                 raise SchemaError, 'class instance %s, missing required attribute %s' % (self.__class__, a)
  713.                 continue
  714.         
  715.         for a, v in self.attributes.items():
  716.             if type(v) is dict:
  717.                 continue
  718.             
  719.             if a in (XMLSchemaComponent.xmlns, XMLNS.XML):
  720.                 continue
  721.             
  722.             if a not in self.__class__.attributes.keys():
  723.                 if self.isAttribute():
  724.                     pass
  725.                 if not self.isReference():
  726.                     raise SchemaError, '%s, unknown attribute(%s,%s)' % (self.getItemTrace(), a, self.attributes[a])
  727.                     continue
  728.         
  729.  
  730.  
  731.  
  732. class WSDLToolsAdapter(XMLSchemaComponent):
  733.     attributes = {
  734.         'name': None,
  735.         'targetNamespace': None }
  736.     tag = 'definitions'
  737.     
  738.     def __init__(self, wsdl):
  739.         XMLSchemaComponent.__init__(self, parent = wsdl)
  740.         self.setAttributes(DOMAdapter(wsdl.document))
  741.  
  742.     
  743.     def getImportSchemas(self):
  744.         return self._parent().types
  745.  
  746.  
  747.  
  748. class Notation(XMLSchemaComponent):
  749.     required = [
  750.         'name',
  751.         'public']
  752.     attributes = {
  753.         'id': None,
  754.         'name': None,
  755.         'public': None,
  756.         'system': None }
  757.     contents = {
  758.         'xsd': 'annotation' }
  759.     tag = 'notation'
  760.     
  761.     def __init__(self, parent):
  762.         XMLSchemaComponent.__init__(self, parent)
  763.         self.annotation = None
  764.  
  765.     
  766.     def fromDom(self, node):
  767.         self.setAttributes(node)
  768.         contents = self.getContents(node)
  769.         for i in contents:
  770.             component = SplitQName(i.getTagName())[1]
  771.             if component == 'annotation' and not (self.annotation):
  772.                 self.annotation = Annotation(self)
  773.                 self.annotation.fromDom(i)
  774.                 continue
  775.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  776.         
  777.  
  778.  
  779.  
  780. class Annotation(XMLSchemaComponent):
  781.     attributes = {
  782.         'id': None }
  783.     contents = {
  784.         'xsd': ('documentation', 'appinfo') }
  785.     tag = 'annotation'
  786.     
  787.     def __init__(self, parent):
  788.         XMLSchemaComponent.__init__(self, parent)
  789.         self.content = None
  790.  
  791.     
  792.     def fromDom(self, node):
  793.         self.setAttributes(node)
  794.         contents = self.getContents(node)
  795.         content = []
  796.         for i in contents:
  797.             component = SplitQName(i.getTagName())[1]
  798.             if component == 'documentation':
  799.                 continue
  800.                 continue
  801.             if component == 'appinfo':
  802.                 continue
  803.                 continue
  804.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  805.         
  806.         self.content = tuple(content)
  807.  
  808.     
  809.     class Documentation(XMLSchemaComponent):
  810.         attributes = {
  811.             'source': None,
  812.             'xml:lang': None }
  813.         contents = {
  814.             'xsd': ('mixed', 'any') }
  815.         tag = 'documentation'
  816.         
  817.         def __init__(self, parent):
  818.             XMLSchemaComponent.__init__(self, parent)
  819.             self.content = None
  820.  
  821.         
  822.         def fromDom(self, node):
  823.             self.setAttributes(node)
  824.             contents = self.getContents(node)
  825.             content = []
  826.             for i in contents:
  827.                 component = SplitQName(i.getTagName())[1]
  828.                 if component == 'mixed':
  829.                     continue
  830.                     continue
  831.                 if component == 'any':
  832.                     continue
  833.                     continue
  834.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  835.             
  836.             self.content = tuple(content)
  837.  
  838.  
  839.     
  840.     class Appinfo(XMLSchemaComponent):
  841.         attributes = {
  842.             'source': None,
  843.             'anyURI': None }
  844.         contents = {
  845.             'xsd': ('mixed', 'any') }
  846.         tag = 'appinfo'
  847.         
  848.         def __init__(self, parent):
  849.             XMLSchemaComponent.__init__(self, parent)
  850.             self.content = None
  851.  
  852.         
  853.         def fromDom(self, node):
  854.             self.setAttributes(node)
  855.             contents = self.getContents(node)
  856.             content = []
  857.             for i in contents:
  858.                 component = SplitQName(i.getTagName())[1]
  859.                 if component == 'mixed':
  860.                     continue
  861.                     continue
  862.                 if component == 'any':
  863.                     continue
  864.                     continue
  865.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  866.             
  867.             self.content = tuple(content)
  868.  
  869.  
  870.  
  871.  
  872. class XMLSchemaFake:
  873.     
  874.     def __init__(self, element):
  875.         self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
  876.         self.element = element
  877.  
  878.  
  879.  
  880. class XMLSchema(XMLSchemaComponent):
  881.     attributes = {
  882.         'id': None,
  883.         'version': None,
  884.         'xml:lang': None,
  885.         'targetNamespace': None,
  886.         'attributeFormDefault': 'unqualified',
  887.         'elementFormDefault': 'unqualified',
  888.         'blockDefault': None,
  889.         'finalDefault': None }
  890.     contents = {
  891.         'xsd': ('include', 'import', 'redefine', 'annotation', 'attribute', 'attributeGroup', 'complexType', 'element', 'group', 'notation', 'simpleType', 'annotation') }
  892.     empty_namespace = ''
  893.     tag = 'schema'
  894.     
  895.     def __init__(self, parent = None):
  896.         self._XMLSchema__node = None
  897.         self.targetNamespace = None
  898.         XMLSchemaComponent.__init__(self, parent)
  899.         
  900.         f = lambda k: k.attributes['name']
  901.         
  902.         ns = lambda k: k.attributes['namespace']
  903.         
  904.         sl = lambda k: k.attributes['schemaLocation']
  905.         self.includes = Collection(self, key = sl)
  906.         self.imports = Collection(self, key = ns)
  907.         self.elements = Collection(self, key = f)
  908.         self.types = Collection(self, key = f)
  909.         self.attr_decl = Collection(self, key = f)
  910.         self.attr_groups = Collection(self, key = f)
  911.         self.model_groups = Collection(self, key = f)
  912.         self.notations = Collection(self, key = f)
  913.         self._imported_schemas = { }
  914.         self._included_schemas = { }
  915.         self._base_url = None
  916.  
  917.     
  918.     def getNode(self):
  919.         return self._XMLSchema__node
  920.  
  921.     
  922.     def addImportSchema(self, schema):
  923.         if not isinstance(schema, XMLSchema):
  924.             raise TypeError, 'expecting a Schema instance'
  925.         
  926.         if schema.targetNamespace != self.targetNamespace:
  927.             self._imported_schemas[schema.targetNamespace] = schema
  928.         else:
  929.             raise SchemaError, 'import schema bad targetNamespace'
  930.  
  931.     
  932.     def addIncludeSchema(self, schemaLocation, schema):
  933.         if not isinstance(schema, XMLSchema):
  934.             raise TypeError, 'expecting a Schema instance'
  935.         
  936.         if not (schema.targetNamespace) or schema.targetNamespace == self.targetNamespace:
  937.             self._included_schemas[schemaLocation] = schema
  938.         else:
  939.             raise SchemaError, 'include schema bad targetNamespace'
  940.  
  941.     
  942.     def setImportSchemas(self, schema_dict):
  943.         self._imported_schemas = schema_dict
  944.  
  945.     
  946.     def getImportSchemas(self):
  947.         return self._imported_schemas
  948.  
  949.     
  950.     def getSchemaNamespacesToImport(self):
  951.         return tuple(self.includes.keys())
  952.  
  953.     
  954.     def setIncludeSchemas(self, schema_dict):
  955.         self._included_schemas = schema_dict
  956.  
  957.     
  958.     def getIncludeSchemas(self):
  959.         return self._included_schemas
  960.  
  961.     
  962.     def getBaseUrl(self):
  963.         return self._base_url
  964.  
  965.     
  966.     def setBaseUrl(self, url):
  967.         self._base_url = url
  968.  
  969.     
  970.     def getElementFormDefault(self):
  971.         return self.attributes.get('elementFormDefault')
  972.  
  973.     
  974.     def isElementFormDefaultQualified(self):
  975.         return self.attributes.get('elementFormDefault') == 'qualified'
  976.  
  977.     
  978.     def getAttributeFormDefault(self):
  979.         return self.attributes.get('attributeFormDefault')
  980.  
  981.     
  982.     def getBlockDefault(self):
  983.         return self.attributes.get('blockDefault')
  984.  
  985.     
  986.     def getFinalDefault(self):
  987.         return self.attributes.get('finalDefault')
  988.  
  989.     
  990.     def load(self, node, location = None):
  991.         self._XMLSchema__node = node
  992.         pnode = node.getParentNode()
  993.         if pnode:
  994.             pname = SplitQName(pnode.getTagName())[1]
  995.             if pname == 'types':
  996.                 attributes = { }
  997.                 self.setAttributes(pnode)
  998.                 attributes.update(self.attributes)
  999.                 self.setAttributes(node)
  1000.                 for k, v in attributes['xmlns'].items():
  1001.                     if not self.attributes['xmlns'].has_key(k):
  1002.                         self.attributes['xmlns'][k] = v
  1003.                         continue
  1004.                 
  1005.             else:
  1006.                 self.setAttributes(node)
  1007.         else:
  1008.             self.setAttributes(node)
  1009.         self.targetNamespace = self.getTargetNamespace()
  1010.         for childNode in self.getContents(node):
  1011.             component = SplitQName(childNode.getTagName())[1]
  1012.             if component == 'include':
  1013.                 tp = self.__class__.Include(self)
  1014.                 tp.fromDom(childNode)
  1015.                 sl = tp.attributes['schemaLocation']
  1016.                 schema = tp.getSchema()
  1017.                 if not self.getIncludeSchemas().has_key(sl):
  1018.                     self.addIncludeSchema(sl, schema)
  1019.                 
  1020.                 self.includes[sl] = tp
  1021.                 pn = childNode.getParentNode().getNode()
  1022.                 pn.removeChild(childNode.getNode())
  1023.                 for child in schema.getNode().getNode().childNodes:
  1024.                     pn.appendChild(child.cloneNode(1))
  1025.                 
  1026.                 for collection in [
  1027.                     'imports',
  1028.                     'elements',
  1029.                     'types',
  1030.                     'attr_decl',
  1031.                     'attr_groups',
  1032.                     'model_groups',
  1033.                     'notations']:
  1034.                     for k, v in getattr(schema, collection).items():
  1035.                         if not getattr(self, collection).has_key(k):
  1036.                             v._parent = weakref.ref(self)
  1037.                             getattr(self, collection)[k] = v
  1038.                             continue
  1039.                         warnings.warn('Not keeping schema component.')
  1040.                     
  1041.                 
  1042.             if component == 'import':
  1043.                 slocd = SchemaReader.namespaceToSchema
  1044.                 tp = self.__class__.Import(self)
  1045.                 tp.fromDom(childNode)
  1046.                 if not tp.getAttribute('namespace'):
  1047.                     pass
  1048.                 import_ns = self.__class__.empty_namespace
  1049.                 schema = slocd.get(import_ns)
  1050.                 if schema is None:
  1051.                     schema = XMLSchema()
  1052.                     slocd[import_ns] = schema
  1053.                     
  1054.                     try:
  1055.                         tp.loadSchema(schema)
  1056.                     except NoSchemaLocationWarning:
  1057.                         ex = None
  1058.                         del slocd[import_ns]
  1059.                         continue
  1060.                     except SchemaError:
  1061.                         ex = None
  1062.                         warnings.warn('<import namespace="%s">, %s' % (import_ns, 'failed to load schema instance, resort to lazy eval when necessary'))
  1063.                         del slocd[import_ns]
  1064.                         
  1065.                         class _LazyEvalImport('_LazyEvalImport', (str,)):
  1066.                             
  1067.                             def getSchema(namespace):
  1068.                                 schema = slocd.get(namespace)
  1069.                                 if schema is None:
  1070.                                     parent = self._parent()
  1071.                                     wstypes = parent
  1072.                                     if isinstance(parent, WSDLToolsAdapter):
  1073.                                         wstypes = parent.getImportSchemas()
  1074.                                     
  1075.                                     schema = wstypes.get(namespace)
  1076.                                 
  1077.                                 if isinstance(schema, XMLSchema):
  1078.                                     self.imports[namespace] = schema
  1079.                                     return schema
  1080.                                 
  1081.  
  1082.  
  1083.                         self.imports[import_ns] = _LazyEvalImport(import_ns)
  1084.                         continue
  1085.                     except:
  1086.                         None<EXCEPTION MATCH>NoSchemaLocationWarning
  1087.                     
  1088.  
  1089.                 None<EXCEPTION MATCH>NoSchemaLocationWarning
  1090.                 tp._schema = schema
  1091.                 if self.getImportSchemas().has_key(import_ns):
  1092.                     warnings.warn('Detected multiple imports of the namespace "%s" ' % import_ns)
  1093.                 
  1094.                 self.addImportSchema(schema)
  1095.                 self.imports[import_ns] = tp
  1096.                 continue
  1097.             if component == 'redefine':
  1098.                 warnings.warn('redefine is ignored')
  1099.                 continue
  1100.             if component == 'annotation':
  1101.                 warnings.warn('annotation is ignored')
  1102.                 continue
  1103.             if component == 'attribute':
  1104.                 tp = AttributeDeclaration(self)
  1105.                 tp.fromDom(childNode)
  1106.                 self.attr_decl[tp.getAttribute('name')] = tp
  1107.                 continue
  1108.             if component == 'attributeGroup':
  1109.                 tp = AttributeGroupDefinition(self)
  1110.                 tp.fromDom(childNode)
  1111.                 self.attr_groups[tp.getAttribute('name')] = tp
  1112.                 continue
  1113.             if component == 'element':
  1114.                 tp = ElementDeclaration(self)
  1115.                 tp.fromDom(childNode)
  1116.                 self.elements[tp.getAttribute('name')] = tp
  1117.                 continue
  1118.             if component == 'group':
  1119.                 tp = ModelGroupDefinition(self)
  1120.                 tp.fromDom(childNode)
  1121.                 self.model_groups[tp.getAttribute('name')] = tp
  1122.                 continue
  1123.             if component == 'notation':
  1124.                 tp = Notation(self)
  1125.                 tp.fromDom(childNode)
  1126.                 self.notations[tp.getAttribute('name')] = tp
  1127.                 continue
  1128.             if component == 'complexType':
  1129.                 tp = ComplexType(self)
  1130.                 tp.fromDom(childNode)
  1131.                 self.types[tp.getAttribute('name')] = tp
  1132.                 continue
  1133.             if component == 'simpleType':
  1134.                 tp = SimpleType(self)
  1135.                 tp.fromDom(childNode)
  1136.                 self.types[tp.getAttribute('name')] = tp
  1137.                 continue
  1138.         
  1139.  
  1140.     
  1141.     class Import(XMLSchemaComponent):
  1142.         attributes = {
  1143.             'id': None,
  1144.             'namespace': None,
  1145.             'schemaLocation': None }
  1146.         contents = {
  1147.             'xsd': [
  1148.                 'annotation'] }
  1149.         tag = 'import'
  1150.         
  1151.         def __init__(self, parent):
  1152.             XMLSchemaComponent.__init__(self, parent)
  1153.             self.annotation = None
  1154.             self._schema = None
  1155.  
  1156.         
  1157.         def fromDom(self, node):
  1158.             self.setAttributes(node)
  1159.             contents = self.getContents(node)
  1160.             if self.attributes['namespace'] == self.getTargetNamespace():
  1161.                 raise SchemaError, 'namespace of schema and import match'
  1162.             
  1163.             for i in contents:
  1164.                 component = SplitQName(i.getTagName())[1]
  1165.                 if component == 'annotation' and not (self.annotation):
  1166.                     self.annotation = Annotation(self)
  1167.                     self.annotation.fromDom(i)
  1168.                     continue
  1169.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1170.             
  1171.  
  1172.         
  1173.         def getSchema(self):
  1174.             if not self._schema:
  1175.                 ns = self.attributes['namespace']
  1176.                 schema = self._parent().getImportSchemas().get(ns)
  1177.                 if not schema and self._parent()._parent:
  1178.                     schema = self._parent()._parent().getImportSchemas().get(ns)
  1179.                 
  1180.                 if not schema:
  1181.                     url = self.attributes.get('schemaLocation')
  1182.                     if not url:
  1183.                         raise SchemaError, 'namespace(%s) is unknown' % ns
  1184.                     
  1185.                     base_url = self._parent().getBaseUrl()
  1186.                     reader = SchemaReader(base_url = base_url)
  1187.                     reader._imports = self._parent().getImportSchemas()
  1188.                     reader._includes = self._parent().getIncludeSchemas()
  1189.                     self._schema = reader.loadFromURL(url)
  1190.                 
  1191.             
  1192.             if not self._schema:
  1193.                 pass
  1194.             return schema
  1195.  
  1196.         
  1197.         def loadSchema(self, schema):
  1198.             base_url = self._parent().getBaseUrl()
  1199.             reader = SchemaReader(base_url = base_url)
  1200.             reader._imports = self._parent().getImportSchemas()
  1201.             reader._includes = self._parent().getIncludeSchemas()
  1202.             self._schema = schema
  1203.             if not self.attributes.has_key('schemaLocation'):
  1204.                 raise NoSchemaLocationWarning('no schemaLocation attribute in import')
  1205.             
  1206.             reader.loadFromURL(self.attributes.get('schemaLocation'), schema)
  1207.  
  1208.  
  1209.     
  1210.     class Include(XMLSchemaComponent):
  1211.         required = [
  1212.             'schemaLocation']
  1213.         attributes = {
  1214.             'id': None,
  1215.             'schemaLocation': None }
  1216.         contents = {
  1217.             'xsd': [
  1218.                 'annotation'] }
  1219.         tag = 'include'
  1220.         
  1221.         def __init__(self, parent):
  1222.             XMLSchemaComponent.__init__(self, parent)
  1223.             self.annotation = None
  1224.             self._schema = None
  1225.  
  1226.         
  1227.         def fromDom(self, node):
  1228.             self.setAttributes(node)
  1229.             contents = self.getContents(node)
  1230.             for i in contents:
  1231.                 component = SplitQName(i.getTagName())[1]
  1232.                 if component == 'annotation' and not (self.annotation):
  1233.                     self.annotation = Annotation(self)
  1234.                     self.annotation.fromDom(i)
  1235.                     continue
  1236.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1237.             
  1238.  
  1239.         
  1240.         def getSchema(self):
  1241.             if not self._schema:
  1242.                 schema = self._parent()
  1243.                 self._schema = schema.getIncludeSchemas().get(self.attributes['schemaLocation'])
  1244.                 if not self._schema:
  1245.                     url = self.attributes['schemaLocation']
  1246.                     reader = SchemaReader(base_url = schema.getBaseUrl())
  1247.                     reader._imports = schema.getImportSchemas()
  1248.                     reader._includes = schema.getIncludeSchemas()
  1249.                     self._schema = XMLSchema(schema)
  1250.                     reader.loadFromURL(url, self._schema)
  1251.                 
  1252.             
  1253.             return self._schema
  1254.  
  1255.  
  1256.  
  1257.  
  1258. class AttributeDeclaration(XMLSchemaComponent, AttributeMarker, DeclarationMarker):
  1259.     required = [
  1260.         'name']
  1261.     attributes = {
  1262.         'id': None,
  1263.         'name': None,
  1264.         'type': None,
  1265.         'default': None,
  1266.         'fixed': None }
  1267.     contents = {
  1268.         'xsd': [
  1269.             'annotation',
  1270.             'simpleType'] }
  1271.     tag = 'attribute'
  1272.     
  1273.     def __init__(self, parent):
  1274.         XMLSchemaComponent.__init__(self, parent)
  1275.         self.annotation = None
  1276.         self.content = None
  1277.  
  1278.     
  1279.     def fromDom(self, node):
  1280.         self.setAttributes(node)
  1281.         contents = self.getContents(node)
  1282.         for i in contents:
  1283.             component = SplitQName(i.getTagName())[1]
  1284.             if component == 'annotation' and not (self.annotation):
  1285.                 self.annotation = Annotation(self)
  1286.                 self.annotation.fromDom(i)
  1287.                 continue
  1288.             if component == 'simpleType':
  1289.                 self.content = AnonymousSimpleType(self)
  1290.                 self.content.fromDom(i)
  1291.                 continue
  1292.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1293.         
  1294.  
  1295.  
  1296.  
  1297. class LocalAttributeDeclaration(AttributeDeclaration, AttributeMarker, LocalMarker, DeclarationMarker):
  1298.     required = [
  1299.         'name']
  1300.     attributes = {
  1301.         'id': None,
  1302.         'name': None,
  1303.         'type': None,
  1304.         'form': (lambda self: GetSchema(self).getAttributeFormDefault()),
  1305.         'use': 'optional',
  1306.         'default': None,
  1307.         'fixed': None }
  1308.     contents = {
  1309.         'xsd': [
  1310.             'annotation',
  1311.             'simpleType'] }
  1312.     
  1313.     def __init__(self, parent):
  1314.         AttributeDeclaration.__init__(self, parent)
  1315.         self.annotation = None
  1316.         self.content = None
  1317.  
  1318.     
  1319.     def fromDom(self, node):
  1320.         self.setAttributes(node)
  1321.         contents = self.getContents(node)
  1322.         for i in contents:
  1323.             component = SplitQName(i.getTagName())[1]
  1324.             if component == 'annotation' and not (self.annotation):
  1325.                 self.annotation = Annotation(self)
  1326.                 self.annotation.fromDom(i)
  1327.                 continue
  1328.             if component == 'simpleType':
  1329.                 self.content = AnonymousSimpleType(self)
  1330.                 self.content.fromDom(i)
  1331.                 continue
  1332.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1333.         
  1334.  
  1335.  
  1336.  
  1337. class AttributeWildCard(XMLSchemaComponent, AttributeMarker, DeclarationMarker, WildCardMarker):
  1338.     attributes = {
  1339.         'id': None,
  1340.         'namespace': '##any',
  1341.         'processContents': 'strict' }
  1342.     contents = {
  1343.         'xsd': [
  1344.             'annotation'] }
  1345.     tag = 'anyAttribute'
  1346.     
  1347.     def __init__(self, parent):
  1348.         XMLSchemaComponent.__init__(self, parent)
  1349.         self.annotation = None
  1350.  
  1351.     
  1352.     def fromDom(self, node):
  1353.         self.setAttributes(node)
  1354.         contents = self.getContents(node)
  1355.         for i in contents:
  1356.             component = SplitQName(i.getTagName())[1]
  1357.             if component == 'annotation' and not (self.annotation):
  1358.                 self.annotation = Annotation(self)
  1359.                 self.annotation.fromDom(i)
  1360.                 continue
  1361.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1362.         
  1363.  
  1364.  
  1365.  
  1366. class AttributeReference(XMLSchemaComponent, AttributeMarker, ReferenceMarker):
  1367.     required = [
  1368.         'ref']
  1369.     attributes = {
  1370.         'id': None,
  1371.         'ref': None,
  1372.         'use': 'optional',
  1373.         'default': None,
  1374.         'fixed': None }
  1375.     contents = {
  1376.         'xsd': [
  1377.             'annotation'] }
  1378.     tag = 'attribute'
  1379.     
  1380.     def __init__(self, parent):
  1381.         XMLSchemaComponent.__init__(self, parent)
  1382.         self.annotation = None
  1383.  
  1384.     
  1385.     def getAttributeDeclaration(self, attribute = 'ref'):
  1386.         return XMLSchemaComponent.getAttributeDeclaration(self, attribute)
  1387.  
  1388.     
  1389.     def fromDom(self, node):
  1390.         self.setAttributes(node)
  1391.         contents = self.getContents(node)
  1392.         for i in contents:
  1393.             component = SplitQName(i.getTagName())[1]
  1394.             if component == 'annotation' and not (self.annotation):
  1395.                 self.annotation = Annotation(self)
  1396.                 self.annotation.fromDom(i)
  1397.                 continue
  1398.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1399.         
  1400.  
  1401.  
  1402.  
  1403. class AttributeGroupDefinition(XMLSchemaComponent, AttributeGroupMarker, DefinitionMarker):
  1404.     required = [
  1405.         'name']
  1406.     attributes = {
  1407.         'id': None,
  1408.         'name': None }
  1409.     contents = {
  1410.         'xsd': [
  1411.             'annotation',
  1412.             'attribute',
  1413.             'attributeGroup',
  1414.             'anyAttribute'] }
  1415.     tag = 'attributeGroup'
  1416.     
  1417.     def __init__(self, parent):
  1418.         XMLSchemaComponent.__init__(self, parent)
  1419.         self.annotation = None
  1420.         self.attr_content = None
  1421.  
  1422.     
  1423.     def getAttributeContent(self):
  1424.         return self.attr_content
  1425.  
  1426.     
  1427.     def fromDom(self, node):
  1428.         self.setAttributes(node)
  1429.         contents = self.getContents(node)
  1430.         content = []
  1431.         for indx in range(len(contents)):
  1432.             component = SplitQName(contents[indx].getTagName())[1]
  1433.             if component == 'annotation' and not indx:
  1434.                 self.annotation = Annotation(self)
  1435.                 self.annotation.fromDom(contents[indx])
  1436.                 continue
  1437.             if component == 'attribute':
  1438.                 if contents[indx].hasattr('name'):
  1439.                     content.append(LocalAttributeDeclaration(self))
  1440.                 elif contents[indx].hasattr('ref'):
  1441.                     content.append(AttributeReference(self))
  1442.                 else:
  1443.                     raise SchemaError, 'Unknown attribute type'
  1444.                 content[-1].fromDom(contents[indx])
  1445.                 continue
  1446.             if component == 'attributeGroup':
  1447.                 content.append(AttributeGroupReference(self))
  1448.                 content[-1].fromDom(contents[indx])
  1449.                 continue
  1450.             if component == 'anyAttribute':
  1451.                 if len(contents) != indx + 1:
  1452.                     raise SchemaError, 'anyAttribute is out of order in %s' % self.getItemTrace()
  1453.                 
  1454.                 content.append(AttributeWildCard(self))
  1455.                 content[-1].fromDom(contents[indx])
  1456.                 continue
  1457.             raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  1458.         
  1459.         self.attr_content = tuple(content)
  1460.  
  1461.  
  1462.  
  1463. class AttributeGroupReference(XMLSchemaComponent, AttributeGroupMarker, ReferenceMarker):
  1464.     required = [
  1465.         'ref']
  1466.     attributes = {
  1467.         'id': None,
  1468.         'ref': None }
  1469.     contents = {
  1470.         'xsd': [
  1471.             'annotation'] }
  1472.     tag = 'attributeGroup'
  1473.     
  1474.     def __init__(self, parent):
  1475.         XMLSchemaComponent.__init__(self, parent)
  1476.         self.annotation = None
  1477.  
  1478.     
  1479.     def getAttributeGroup(self, attribute = 'ref'):
  1480.         return XMLSchemaComponent.getAttributeGroup(self, attribute)
  1481.  
  1482.     
  1483.     def fromDom(self, node):
  1484.         self.setAttributes(node)
  1485.         contents = self.getContents(node)
  1486.         for i in contents:
  1487.             component = SplitQName(i.getTagName())[1]
  1488.             if component == 'annotation' and not (self.annotation):
  1489.                 self.annotation = Annotation(self)
  1490.                 self.annotation.fromDom(i)
  1491.                 continue
  1492.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1493.         
  1494.  
  1495.  
  1496.  
  1497. class IdentityConstrants(XMLSchemaComponent):
  1498.     
  1499.     def __init__(self, parent):
  1500.         XMLSchemaComponent.__init__(self, parent)
  1501.         self.selector = None
  1502.         self.fields = None
  1503.         self.annotation = None
  1504.  
  1505.     
  1506.     def fromDom(self, node):
  1507.         self.setAttributes(node)
  1508.         contents = self.getContents(node)
  1509.         fields = []
  1510.         for i in contents:
  1511.             component = SplitQName(i.getTagName())[1]
  1512.             if component in self.__class__.contents['xsd']:
  1513.                 if component == 'annotation' and not (self.annotation):
  1514.                     self.annotation = Annotation(self)
  1515.                     self.annotation.fromDom(i)
  1516.                 elif component == 'selector':
  1517.                     self.selector = self.Selector(self)
  1518.                     self.selector.fromDom(i)
  1519.                     continue
  1520.                 elif component == 'field':
  1521.                     fields.append(self.Field(self))
  1522.                     fields[-1].fromDom(i)
  1523.                     continue
  1524.                 else:
  1525.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1526.             else:
  1527.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1528.             self.fields = tuple(fields)
  1529.         
  1530.  
  1531.     
  1532.     class Constraint(XMLSchemaComponent):
  1533.         
  1534.         def __init__(self, parent):
  1535.             XMLSchemaComponent.__init__(self, parent)
  1536.             self.annotation = None
  1537.  
  1538.         
  1539.         def fromDom(self, node):
  1540.             self.setAttributes(node)
  1541.             contents = self.getContents(node)
  1542.             for i in contents:
  1543.                 component = SplitQName(i.getTagName())[1]
  1544.                 if component in self.__class__.contents['xsd']:
  1545.                     if component == 'annotation' and not (self.annotation):
  1546.                         self.annotation = Annotation(self)
  1547.                         self.annotation.fromDom(i)
  1548.                     else:
  1549.                         raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1550.                 not (self.annotation)
  1551.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1552.             
  1553.  
  1554.  
  1555.     
  1556.     class Selector(Constraint):
  1557.         required = [
  1558.             'xpath']
  1559.         attributes = {
  1560.             'id': None,
  1561.             'xpath': None }
  1562.         contents = {
  1563.             'xsd': [
  1564.                 'annotation'] }
  1565.         tag = 'selector'
  1566.  
  1567.     
  1568.     class Field(Constraint):
  1569.         required = [
  1570.             'xpath']
  1571.         attributes = {
  1572.             'id': None,
  1573.             'xpath': None }
  1574.         contents = {
  1575.             'xsd': [
  1576.                 'annotation'] }
  1577.         tag = 'field'
  1578.  
  1579.  
  1580.  
  1581. class Unique(IdentityConstrants):
  1582.     required = [
  1583.         'name']
  1584.     attributes = {
  1585.         'id': None,
  1586.         'name': None }
  1587.     contents = {
  1588.         'xsd': [
  1589.             'annotation',
  1590.             'selector',
  1591.             'field'] }
  1592.     tag = 'unique'
  1593.  
  1594.  
  1595. class Key(IdentityConstrants):
  1596.     required = [
  1597.         'name']
  1598.     attributes = {
  1599.         'id': None,
  1600.         'name': None }
  1601.     contents = {
  1602.         'xsd': [
  1603.             'annotation',
  1604.             'selector',
  1605.             'field'] }
  1606.     tag = 'key'
  1607.  
  1608.  
  1609. class KeyRef(IdentityConstrants):
  1610.     required = [
  1611.         'name',
  1612.         'refer']
  1613.     attributes = {
  1614.         'id': None,
  1615.         'name': None,
  1616.         'refer': None }
  1617.     contents = {
  1618.         'xsd': [
  1619.             'annotation',
  1620.             'selector',
  1621.             'field'] }
  1622.     tag = 'keyref'
  1623.  
  1624.  
  1625. class ElementDeclaration(XMLSchemaComponent, ElementMarker, DeclarationMarker):
  1626.     required = [
  1627.         'name']
  1628.     attributes = {
  1629.         'id': None,
  1630.         'name': None,
  1631.         'type': None,
  1632.         'default': None,
  1633.         'fixed': None,
  1634.         'nillable': 0,
  1635.         'abstract': 0,
  1636.         'substitutionGroup': None,
  1637.         'block': (lambda self: self._parent().getBlockDefault()),
  1638.         'final': (lambda self: self._parent().getFinalDefault()) }
  1639.     contents = {
  1640.         'xsd': [
  1641.             'annotation',
  1642.             'simpleType',
  1643.             'complexType',
  1644.             'key',
  1645.             'keyref',
  1646.             'unique'] }
  1647.     tag = 'element'
  1648.     
  1649.     def __init__(self, parent):
  1650.         XMLSchemaComponent.__init__(self, parent)
  1651.         self.annotation = None
  1652.         self.content = None
  1653.         self.constraints = ()
  1654.  
  1655.     
  1656.     def isQualified(self):
  1657.         return True
  1658.  
  1659.     
  1660.     def getAttribute(self, attribute):
  1661.         value = XMLSchemaComponent.getAttribute(self, attribute)
  1662.         if attribute != 'type' or value is not None:
  1663.             return value
  1664.         
  1665.         if self.content is not None:
  1666.             return None
  1667.         
  1668.         parent = self
  1669.         while None:
  1670.             nsdict = parent.attributes[XMLSchemaComponent.xmlns]
  1671.             for k, v in nsdict.items():
  1672.                 if v not in SCHEMA.XSD_LIST:
  1673.                     continue
  1674.                 
  1675.                 return TypeDescriptionComponent((v, 'anyType'))
  1676.             
  1677.             if isinstance(parent, WSDLToolsAdapter) or not hasattr(parent, '_parent'):
  1678.                 break
  1679.             
  1680.             parent = parent._parent()
  1681.             continue
  1682.             raise SchemaError, 'failed to locate the XSD namespace'
  1683.             return None
  1684.  
  1685.     
  1686.     def getElementDeclaration(self, attribute):
  1687.         raise Warning, 'invalid operation for <%s>' % self.tag
  1688.  
  1689.     
  1690.     def getTypeDefinition(self, attribute = None):
  1691.         if attribute:
  1692.             return XMLSchemaComponent.getTypeDefinition(self, attribute)
  1693.         
  1694.         gt = XMLSchemaComponent.getTypeDefinition(self, 'type')
  1695.         if gt:
  1696.             return gt
  1697.         
  1698.         return self.content
  1699.  
  1700.     
  1701.     def getConstraints(self):
  1702.         return self._constraints
  1703.  
  1704.     
  1705.     def setConstraints(self, constraints):
  1706.         self._constraints = tuple(constraints)
  1707.  
  1708.     constraints = property(getConstraints, setConstraints, None, 'tuple of key, keyref, unique constraints')
  1709.     
  1710.     def fromDom(self, node):
  1711.         self.setAttributes(node)
  1712.         contents = self.getContents(node)
  1713.         constraints = []
  1714.         for i in contents:
  1715.             component = SplitQName(i.getTagName())[1]
  1716.             if component in self.__class__.contents['xsd']:
  1717.                 if component == 'annotation' and not (self.annotation):
  1718.                     self.annotation = Annotation(self)
  1719.                     self.annotation.fromDom(i)
  1720.                 elif component == 'simpleType' and not (self.content):
  1721.                     self.content = AnonymousSimpleType(self)
  1722.                     self.content.fromDom(i)
  1723.                 elif component == 'complexType' and not (self.content):
  1724.                     self.content = LocalComplexType(self)
  1725.                     self.content.fromDom(i)
  1726.                 elif component == 'key':
  1727.                     constraints.append(Key(self))
  1728.                     constraints[-1].fromDom(i)
  1729.                 elif component == 'keyref':
  1730.                     constraints.append(KeyRef(self))
  1731.                     constraints[-1].fromDom(i)
  1732.                 elif component == 'unique':
  1733.                     constraints.append(Unique(self))
  1734.                     constraints[-1].fromDom(i)
  1735.                 else:
  1736.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1737.             not (self.content)
  1738.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1739.         
  1740.         self.constraints = constraints
  1741.  
  1742.  
  1743.  
  1744. class LocalElementDeclaration(ElementDeclaration, LocalMarker):
  1745.     required = [
  1746.         'name']
  1747.     attributes = {
  1748.         'id': None,
  1749.         'name': None,
  1750.         'form': (lambda self: GetSchema(self).getElementFormDefault()),
  1751.         'type': None,
  1752.         'minOccurs': '1',
  1753.         'maxOccurs': '1',
  1754.         'default': None,
  1755.         'fixed': None,
  1756.         'nillable': 0,
  1757.         'abstract': 0,
  1758.         'block': (lambda self: GetSchema(self).getBlockDefault()) }
  1759.     contents = {
  1760.         'xsd': [
  1761.             'annotation',
  1762.             'simpleType',
  1763.             'complexType',
  1764.             'key',
  1765.             'keyref',
  1766.             'unique'] }
  1767.     
  1768.     def isQualified(self):
  1769.         form = self.getAttribute('form')
  1770.         if form == 'qualified':
  1771.             return True
  1772.         
  1773.         if form == 'unqualified':
  1774.             return False
  1775.         
  1776.         raise SchemaError, 'Bad form (%s) for element: %s' % (form, self.getItemTrace())
  1777.  
  1778.  
  1779.  
  1780. class ElementReference(XMLSchemaComponent, ElementMarker, ReferenceMarker):
  1781.     required = [
  1782.         'ref']
  1783.     attributes = {
  1784.         'id': None,
  1785.         'ref': None,
  1786.         'minOccurs': '1',
  1787.         'maxOccurs': '1' }
  1788.     contents = {
  1789.         'xsd': [
  1790.             'annotation'] }
  1791.     tag = 'element'
  1792.     
  1793.     def __init__(self, parent):
  1794.         XMLSchemaComponent.__init__(self, parent)
  1795.         self.annotation = None
  1796.  
  1797.     
  1798.     def getElementDeclaration(self, attribute = None):
  1799.         if attribute:
  1800.             return XMLSchemaComponent.getElementDeclaration(self, attribute)
  1801.         
  1802.         return XMLSchemaComponent.getElementDeclaration(self, 'ref')
  1803.  
  1804.     
  1805.     def fromDom(self, node):
  1806.         self.annotation = None
  1807.         self.setAttributes(node)
  1808.         for i in self.getContents(node):
  1809.             component = SplitQName(i.getTagName())[1]
  1810.             if component in self.__class__.contents['xsd']:
  1811.                 if component == 'annotation' and not (self.annotation):
  1812.                     self.annotation = Annotation(self)
  1813.                     self.annotation.fromDom(i)
  1814.                 else:
  1815.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1816.             not (self.annotation)
  1817.         
  1818.  
  1819.  
  1820.  
  1821. class ElementWildCard(LocalElementDeclaration, WildCardMarker):
  1822.     required = []
  1823.     attributes = {
  1824.         'id': None,
  1825.         'minOccurs': '1',
  1826.         'maxOccurs': '1',
  1827.         'namespace': '##any',
  1828.         'processContents': 'strict' }
  1829.     contents = {
  1830.         'xsd': [
  1831.             'annotation'] }
  1832.     tag = 'any'
  1833.     
  1834.     def __init__(self, parent):
  1835.         XMLSchemaComponent.__init__(self, parent)
  1836.         self.annotation = None
  1837.  
  1838.     
  1839.     def isQualified(self):
  1840.         return GetSchema(self).isElementFormDefaultQualified()
  1841.  
  1842.     
  1843.     def getAttribute(self, attribute):
  1844.         return XMLSchemaComponent.getAttribute(self, attribute)
  1845.  
  1846.     
  1847.     def getTypeDefinition(self, attribute):
  1848.         raise Warning, 'invalid operation for <%s>' % self.tag
  1849.  
  1850.     
  1851.     def fromDom(self, node):
  1852.         self.annotation = None
  1853.         self.setAttributes(node)
  1854.         for i in self.getContents(node):
  1855.             component = SplitQName(i.getTagName())[1]
  1856.             if component in self.__class__.contents['xsd']:
  1857.                 if component == 'annotation' and not (self.annotation):
  1858.                     self.annotation = Annotation(self)
  1859.                     self.annotation.fromDom(i)
  1860.                 else:
  1861.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1862.             not (self.annotation)
  1863.         
  1864.  
  1865.  
  1866.  
  1867. class Sequence(XMLSchemaComponent, SequenceMarker):
  1868.     attributes = {
  1869.         'id': None,
  1870.         'minOccurs': '1',
  1871.         'maxOccurs': '1' }
  1872.     contents = {
  1873.         'xsd': [
  1874.             'annotation',
  1875.             'element',
  1876.             'group',
  1877.             'choice',
  1878.             'sequence',
  1879.             'any'] }
  1880.     tag = 'sequence'
  1881.     
  1882.     def __init__(self, parent):
  1883.         XMLSchemaComponent.__init__(self, parent)
  1884.         self.annotation = None
  1885.         self.content = None
  1886.  
  1887.     
  1888.     def fromDom(self, node):
  1889.         self.setAttributes(node)
  1890.         contents = self.getContents(node)
  1891.         content = []
  1892.         for i in contents:
  1893.             component = SplitQName(i.getTagName())[1]
  1894.             if component in self.__class__.contents['xsd']:
  1895.                 if component == 'annotation' and not (self.annotation):
  1896.                     self.annotation = Annotation(self)
  1897.                     self.annotation.fromDom(i)
  1898.                     continue
  1899.                 elif component == 'element':
  1900.                     if i.hasattr('ref'):
  1901.                         content.append(ElementReference(self))
  1902.                     else:
  1903.                         content.append(LocalElementDeclaration(self))
  1904.                 elif component == 'group':
  1905.                     content.append(ModelGroupReference(self))
  1906.                 elif component == 'choice':
  1907.                     content.append(Choice(self))
  1908.                 elif component == 'sequence':
  1909.                     content.append(Sequence(self))
  1910.                 elif component == 'any':
  1911.                     content.append(ElementWildCard(self))
  1912.                 else:
  1913.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1914.                 content[-1].fromDom(i)
  1915.                 continue
  1916.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1917.         
  1918.         self.content = tuple(content)
  1919.  
  1920.  
  1921.  
  1922. class All(XMLSchemaComponent, AllMarker):
  1923.     attributes = {
  1924.         'id': None,
  1925.         'minOccurs': '1',
  1926.         'maxOccurs': '1' }
  1927.     contents = {
  1928.         'xsd': [
  1929.             'annotation',
  1930.             'element'] }
  1931.     tag = 'all'
  1932.     
  1933.     def __init__(self, parent):
  1934.         XMLSchemaComponent.__init__(self, parent)
  1935.         self.annotation = None
  1936.         self.content = None
  1937.  
  1938.     
  1939.     def fromDom(self, node):
  1940.         self.setAttributes(node)
  1941.         contents = self.getContents(node)
  1942.         content = []
  1943.         for i in contents:
  1944.             component = SplitQName(i.getTagName())[1]
  1945.             if component in self.__class__.contents['xsd']:
  1946.                 if component == 'annotation' and not (self.annotation):
  1947.                     self.annotation = Annotation(self)
  1948.                     self.annotation.fromDom(i)
  1949.                     continue
  1950.                 elif component == 'element':
  1951.                     if i.hasattr('ref'):
  1952.                         content.append(ElementReference(self))
  1953.                     else:
  1954.                         content.append(LocalElementDeclaration(self))
  1955.                 else:
  1956.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1957.                 content[-1].fromDom(i)
  1958.                 continue
  1959.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1960.         
  1961.         self.content = tuple(content)
  1962.  
  1963.  
  1964.  
  1965. class Choice(XMLSchemaComponent, ChoiceMarker):
  1966.     attributes = {
  1967.         'id': None,
  1968.         'minOccurs': '1',
  1969.         'maxOccurs': '1' }
  1970.     contents = {
  1971.         'xsd': [
  1972.             'annotation',
  1973.             'element',
  1974.             'group',
  1975.             'choice',
  1976.             'sequence',
  1977.             'any'] }
  1978.     tag = 'choice'
  1979.     
  1980.     def __init__(self, parent):
  1981.         XMLSchemaComponent.__init__(self, parent)
  1982.         self.annotation = None
  1983.         self.content = None
  1984.  
  1985.     
  1986.     def fromDom(self, node):
  1987.         self.setAttributes(node)
  1988.         contents = self.getContents(node)
  1989.         content = []
  1990.         for i in contents:
  1991.             component = SplitQName(i.getTagName())[1]
  1992.             if component in self.__class__.contents['xsd']:
  1993.                 if component == 'annotation' and not (self.annotation):
  1994.                     self.annotation = Annotation(self)
  1995.                     self.annotation.fromDom(i)
  1996.                     continue
  1997.                 elif component == 'element':
  1998.                     if i.hasattr('ref'):
  1999.                         content.append(ElementReference(self))
  2000.                     else:
  2001.                         content.append(LocalElementDeclaration(self))
  2002.                 elif component == 'group':
  2003.                     content.append(ModelGroupReference(self))
  2004.                 elif component == 'choice':
  2005.                     content.append(Choice(self))
  2006.                 elif component == 'sequence':
  2007.                     content.append(Sequence(self))
  2008.                 elif component == 'any':
  2009.                     content.append(ElementWildCard(self))
  2010.                 else:
  2011.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2012.                 content[-1].fromDom(i)
  2013.                 continue
  2014.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2015.         
  2016.         self.content = tuple(content)
  2017.  
  2018.  
  2019.  
  2020. class ModelGroupDefinition(XMLSchemaComponent, ModelGroupMarker, DefinitionMarker):
  2021.     required = [
  2022.         'name']
  2023.     attributes = {
  2024.         'id': None,
  2025.         'name': None }
  2026.     contents = {
  2027.         'xsd': [
  2028.             'annotation',
  2029.             'all',
  2030.             'choice',
  2031.             'sequence'] }
  2032.     tag = 'group'
  2033.     
  2034.     def __init__(self, parent):
  2035.         XMLSchemaComponent.__init__(self, parent)
  2036.         self.annotation = None
  2037.         self.content = None
  2038.  
  2039.     
  2040.     def fromDom(self, node):
  2041.         self.setAttributes(node)
  2042.         contents = self.getContents(node)
  2043.         for i in contents:
  2044.             component = SplitQName(i.getTagName())[1]
  2045.             if component in self.__class__.contents['xsd']:
  2046.                 if component == 'annotation' and not (self.annotation):
  2047.                     self.annotation = Annotation(self)
  2048.                     self.annotation.fromDom(i)
  2049.                     continue
  2050.                 elif component == 'all' and not (self.content):
  2051.                     self.content = All(self)
  2052.                 elif component == 'choice' and not (self.content):
  2053.                     self.content = Choice(self)
  2054.                 elif component == 'sequence' and not (self.content):
  2055.                     self.content = Sequence(self)
  2056.                 else:
  2057.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2058.                 self.content.fromDom(i)
  2059.                 continue
  2060.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2061.         
  2062.  
  2063.  
  2064.  
  2065. class ModelGroupReference(XMLSchemaComponent, ModelGroupMarker, ReferenceMarker):
  2066.     required = [
  2067.         'ref']
  2068.     attributes = {
  2069.         'id': None,
  2070.         'ref': None,
  2071.         'minOccurs': '1',
  2072.         'maxOccurs': '1' }
  2073.     contents = {
  2074.         'xsd': [
  2075.             'annotation'] }
  2076.     tag = 'group'
  2077.     
  2078.     def __init__(self, parent):
  2079.         XMLSchemaComponent.__init__(self, parent)
  2080.         self.annotation = None
  2081.  
  2082.     
  2083.     def getModelGroupReference(self):
  2084.         return self.getModelGroup('ref')
  2085.  
  2086.     
  2087.     def fromDom(self, node):
  2088.         self.setAttributes(node)
  2089.         contents = self.getContents(node)
  2090.         for i in contents:
  2091.             component = SplitQName(i.getTagName())[1]
  2092.             if component in self.__class__.contents['xsd']:
  2093.                 if component == 'annotation' and not (self.annotation):
  2094.                     self.annotation = Annotation(self)
  2095.                     self.annotation.fromDom(i)
  2096.                 else:
  2097.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2098.             not (self.annotation)
  2099.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2100.         
  2101.  
  2102.  
  2103.  
  2104. class ComplexType(XMLSchemaComponent, DefinitionMarker, ComplexMarker):
  2105.     required = [
  2106.         'name']
  2107.     attributes = {
  2108.         'id': None,
  2109.         'name': None,
  2110.         'mixed': 0,
  2111.         'abstract': 0,
  2112.         'block': (lambda self: self._parent().getBlockDefault()),
  2113.         'final': (lambda self: self._parent().getFinalDefault()) }
  2114.     contents = {
  2115.         'xsd': [
  2116.             'annotation',
  2117.             'simpleContent',
  2118.             'complexContent',
  2119.             'group',
  2120.             'all',
  2121.             'choice',
  2122.             'sequence',
  2123.             'attribute',
  2124.             'attributeGroup',
  2125.             'anyAttribute',
  2126.             'any'] }
  2127.     tag = 'complexType'
  2128.     
  2129.     def __init__(self, parent):
  2130.         XMLSchemaComponent.__init__(self, parent)
  2131.         self.annotation = None
  2132.         self.content = None
  2133.         self.attr_content = None
  2134.  
  2135.     
  2136.     def isMixed(self):
  2137.         m = self.getAttribute('mixed')
  2138.         if m == 0 or m == False:
  2139.             return False
  2140.         
  2141.         if isinstance(m, basestring) is True:
  2142.             if m in ('false', '0'):
  2143.                 return False
  2144.             
  2145.             if m in ('true', '1'):
  2146.                 return True
  2147.             
  2148.         
  2149.         raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
  2150.  
  2151.     
  2152.     def getAttributeContent(self):
  2153.         return self.attr_content
  2154.  
  2155.     
  2156.     def getElementDeclaration(self, attribute):
  2157.         raise Warning, 'invalid operation for <%s>' % self.tag
  2158.  
  2159.     
  2160.     def getTypeDefinition(self, attribute):
  2161.         raise Warning, 'invalid operation for <%s>' % self.tag
  2162.  
  2163.     
  2164.     def fromDom(self, node):
  2165.         self.setAttributes(node)
  2166.         contents = self.getContents(node)
  2167.         indx = 0
  2168.         num = len(contents)
  2169.         if not num:
  2170.             return None
  2171.         
  2172.         component = SplitQName(contents[indx].getTagName())[1]
  2173.         if component == 'annotation':
  2174.             self.annotation = Annotation(self)
  2175.             self.annotation.fromDom(contents[indx])
  2176.             indx += 1
  2177.             component = SplitQName(contents[indx].getTagName())[1]
  2178.         
  2179.         self.content = None
  2180.         if component == 'simpleContent':
  2181.             self.content = self.__class__.SimpleContent(self)
  2182.             self.content.fromDom(contents[indx])
  2183.         elif component == 'complexContent':
  2184.             self.content = self.__class__.ComplexContent(self)
  2185.             self.content.fromDom(contents[indx])
  2186.         elif component == 'all':
  2187.             self.content = All(self)
  2188.         elif component == 'choice':
  2189.             self.content = Choice(self)
  2190.         elif component == 'sequence':
  2191.             self.content = Sequence(self)
  2192.         elif component == 'group':
  2193.             self.content = ModelGroupReference(self)
  2194.         
  2195.         if self.content:
  2196.             self.content.fromDom(contents[indx])
  2197.             indx += 1
  2198.         
  2199.         self.attr_content = []
  2200.         while indx < num:
  2201.             component = SplitQName(contents[indx].getTagName())[1]
  2202.             if component == 'attribute':
  2203.                 if contents[indx].hasattr('ref'):
  2204.                     self.attr_content.append(AttributeReference(self))
  2205.                 else:
  2206.                     self.attr_content.append(LocalAttributeDeclaration(self))
  2207.             elif component == 'attributeGroup':
  2208.                 self.attr_content.append(AttributeGroupReference(self))
  2209.             elif component == 'anyAttribute':
  2210.                 self.attr_content.append(AttributeWildCard(self))
  2211.             else:
  2212.                 raise SchemaError, 'Unknown component (%s): %s' % (contents[indx].getTagName(), self.getItemTrace())
  2213.             self.attr_content[-1].fromDom(contents[indx])
  2214.             indx += 1
  2215.  
  2216.     
  2217.     class _DerivedType(XMLSchemaComponent):
  2218.         
  2219.         def __init__(self, parent):
  2220.             XMLSchemaComponent.__init__(self, parent)
  2221.             self.annotation = None
  2222.             self.derivation = None
  2223.             self.content = None
  2224.  
  2225.         
  2226.         def fromDom(self, node):
  2227.             self.setAttributes(node)
  2228.             contents = self.getContents(node)
  2229.             for i in contents:
  2230.                 component = SplitQName(i.getTagName())[1]
  2231.                 if component in self.__class__.contents['xsd']:
  2232.                     if component == 'annotation' and not (self.annotation):
  2233.                         self.annotation = Annotation(self)
  2234.                         self.annotation.fromDom(i)
  2235.                         continue
  2236.                     elif component == 'restriction' and not (self.derivation):
  2237.                         self.derivation = self.__class__.Restriction(self)
  2238.                     elif component == 'extension' and not (self.derivation):
  2239.                         self.derivation = self.__class__.Extension(self)
  2240.                     else:
  2241.                         raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2242.                 else:
  2243.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2244.                 self.derivation.fromDom(i)
  2245.             
  2246.             self.content = self.derivation
  2247.  
  2248.  
  2249.     
  2250.     class ComplexContent(_DerivedType, ComplexMarker):
  2251.         attributes = {
  2252.             'id': None,
  2253.             'mixed': 0 }
  2254.         contents = {
  2255.             'xsd': [
  2256.                 'annotation',
  2257.                 'restriction',
  2258.                 'extension'] }
  2259.         tag = 'complexContent'
  2260.         
  2261.         def isMixed(self):
  2262.             m = self.getAttribute('mixed')
  2263.             if m == 0 or m == False:
  2264.                 return False
  2265.             
  2266.             if isinstance(m, basestring) is True:
  2267.                 if m in ('false', '0'):
  2268.                     return False
  2269.                 
  2270.                 if m in ('true', '1'):
  2271.                     return True
  2272.                 
  2273.             
  2274.             raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
  2275.  
  2276.         
  2277.         class _DerivationBase(XMLSchemaComponent):
  2278.             required = [
  2279.                 'base']
  2280.             attributes = {
  2281.                 'id': None,
  2282.                 'base': None }
  2283.             contents = {
  2284.                 'xsd': [
  2285.                     'annotation',
  2286.                     'group',
  2287.                     'all',
  2288.                     'choice',
  2289.                     'sequence',
  2290.                     'attribute',
  2291.                     'attributeGroup',
  2292.                     'anyAttribute'] }
  2293.             
  2294.             def __init__(self, parent):
  2295.                 XMLSchemaComponent.__init__(self, parent)
  2296.                 self.annotation = None
  2297.                 self.content = None
  2298.                 self.attr_content = None
  2299.  
  2300.             
  2301.             def getAttributeContent(self):
  2302.                 return self.attr_content
  2303.  
  2304.             
  2305.             def fromDom(self, node):
  2306.                 self.setAttributes(node)
  2307.                 contents = self.getContents(node)
  2308.                 indx = 0
  2309.                 num = len(contents)
  2310.                 if not num:
  2311.                     return None
  2312.                 
  2313.                 component = SplitQName(contents[indx].getTagName())[1]
  2314.                 if component == 'annotation':
  2315.                     self.annotation = Annotation(self)
  2316.                     self.annotation.fromDom(contents[indx])
  2317.                     indx += 1
  2318.                     component = SplitQName(contents[indx].getTagName())[1]
  2319.                 
  2320.                 if component == 'all':
  2321.                     self.content = All(self)
  2322.                     self.content.fromDom(contents[indx])
  2323.                     indx += 1
  2324.                 elif component == 'choice':
  2325.                     self.content = Choice(self)
  2326.                     self.content.fromDom(contents[indx])
  2327.                     indx += 1
  2328.                 elif component == 'sequence':
  2329.                     self.content = Sequence(self)
  2330.                     self.content.fromDom(contents[indx])
  2331.                     indx += 1
  2332.                 elif component == 'group':
  2333.                     self.content = ModelGroupReference(self)
  2334.                     self.content.fromDom(contents[indx])
  2335.                     indx += 1
  2336.                 else:
  2337.                     self.content = None
  2338.                 self.attr_content = []
  2339.                 while indx < num:
  2340.                     component = SplitQName(contents[indx].getTagName())[1]
  2341.                     if component == 'attribute':
  2342.                         if contents[indx].hasattr('ref'):
  2343.                             self.attr_content.append(AttributeReference(self))
  2344.                         else:
  2345.                             self.attr_content.append(LocalAttributeDeclaration(self))
  2346.                     elif component == 'attributeGroup':
  2347.                         if contents[indx].hasattr('ref'):
  2348.                             self.attr_content.append(AttributeGroupReference(self))
  2349.                         else:
  2350.                             self.attr_content.append(AttributeGroupDefinition(self))
  2351.                     elif component == 'anyAttribute':
  2352.                         self.attr_content.append(AttributeWildCard(self))
  2353.                     else:
  2354.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2355.                     self.attr_content[-1].fromDom(contents[indx])
  2356.                     indx += 1
  2357.  
  2358.  
  2359.         
  2360.         class Extension(_DerivationBase, ExtensionMarker):
  2361.             tag = 'extension'
  2362.  
  2363.         
  2364.         class Restriction(_DerivationBase, RestrictionMarker):
  2365.             tag = 'restriction'
  2366.  
  2367.  
  2368.     
  2369.     class SimpleContent(_DerivedType, SimpleMarker):
  2370.         attributes = {
  2371.             'id': None }
  2372.         contents = {
  2373.             'xsd': [
  2374.                 'annotation',
  2375.                 'restriction',
  2376.                 'extension'] }
  2377.         tag = 'simpleContent'
  2378.         
  2379.         class Extension(XMLSchemaComponent, ExtensionMarker):
  2380.             required = [
  2381.                 'base']
  2382.             attributes = {
  2383.                 'id': None,
  2384.                 'base': None }
  2385.             contents = {
  2386.                 'xsd': [
  2387.                     'annotation',
  2388.                     'attribute',
  2389.                     'attributeGroup',
  2390.                     'anyAttribute'] }
  2391.             tag = 'extension'
  2392.             
  2393.             def __init__(self, parent):
  2394.                 XMLSchemaComponent.__init__(self, parent)
  2395.                 self.annotation = None
  2396.                 self.attr_content = None
  2397.  
  2398.             
  2399.             def getAttributeContent(self):
  2400.                 return self.attr_content
  2401.  
  2402.             
  2403.             def fromDom(self, node):
  2404.                 self.setAttributes(node)
  2405.                 contents = self.getContents(node)
  2406.                 indx = 0
  2407.                 num = len(contents)
  2408.                 if num:
  2409.                     component = SplitQName(contents[indx].getTagName())[1]
  2410.                     if component == 'annotation':
  2411.                         self.annotation = Annotation(self)
  2412.                         self.annotation.fromDom(contents[indx])
  2413.                         indx += 1
  2414.                         component = SplitQName(contents[indx].getTagName())[1]
  2415.                     
  2416.                 
  2417.                 content = []
  2418.                 while indx < num:
  2419.                     component = SplitQName(contents[indx].getTagName())[1]
  2420.                     if component == 'attribute':
  2421.                         if contents[indx].hasattr('ref'):
  2422.                             content.append(AttributeReference(self))
  2423.                         else:
  2424.                             content.append(LocalAttributeDeclaration(self))
  2425.                     elif component == 'attributeGroup':
  2426.                         content.append(AttributeGroupReference(self))
  2427.                     elif component == 'anyAttribute':
  2428.                         content.append(AttributeWildCard(self))
  2429.                     else:
  2430.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2431.                     content[-1].fromDom(contents[indx])
  2432.                     indx += 1
  2433.                 self.attr_content = tuple(content)
  2434.  
  2435.  
  2436.         
  2437.         class Restriction(XMLSchemaComponent, RestrictionMarker):
  2438.             required = [
  2439.                 'base']
  2440.             attributes = {
  2441.                 'id': None,
  2442.                 'base': None }
  2443.             contents = {
  2444.                 'xsd': [
  2445.                     'annotation',
  2446.                     'simpleType',
  2447.                     'attribute',
  2448.                     'attributeGroup',
  2449.                     'anyAttribute'] + RestrictionMarker.facets }
  2450.             tag = 'restriction'
  2451.             
  2452.             def __init__(self, parent):
  2453.                 XMLSchemaComponent.__init__(self, parent)
  2454.                 self.annotation = None
  2455.                 self.content = None
  2456.                 self.attr_content = None
  2457.  
  2458.             
  2459.             def getAttributeContent(self):
  2460.                 return self.attr_content
  2461.  
  2462.             
  2463.             def fromDom(self, node):
  2464.                 self.content = []
  2465.                 self.setAttributes(node)
  2466.                 contents = self.getContents(node)
  2467.                 indx = 0
  2468.                 num = len(contents)
  2469.                 component = SplitQName(contents[indx].getTagName())[1]
  2470.                 if component == 'annotation':
  2471.                     self.annotation = Annotation(self)
  2472.                     self.annotation.fromDom(contents[indx])
  2473.                     indx += 1
  2474.                     component = SplitQName(contents[indx].getTagName())[1]
  2475.                 
  2476.                 content = []
  2477.                 while indx < num:
  2478.                     component = SplitQName(contents[indx].getTagName())[1]
  2479.                     if component == 'attribute':
  2480.                         if contents[indx].hasattr('ref'):
  2481.                             content.append(AttributeReference(self))
  2482.                         else:
  2483.                             content.append(LocalAttributeDeclaration(self))
  2484.                     elif component == 'attributeGroup':
  2485.                         content.append(AttributeGroupReference(self))
  2486.                     elif component == 'anyAttribute':
  2487.                         content.append(AttributeWildCard(self))
  2488.                     elif component == 'simpleType':
  2489.                         self.content.append(AnonymousSimpleType(self))
  2490.                         self.content[-1].fromDom(contents[indx])
  2491.                     else:
  2492.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2493.                     content[-1].fromDom(contents[indx])
  2494.                     indx += 1
  2495.                 self.attr_content = tuple(content)
  2496.  
  2497.  
  2498.  
  2499.  
  2500.  
  2501. class LocalComplexType(ComplexType, LocalMarker):
  2502.     required = []
  2503.     attributes = {
  2504.         'id': None,
  2505.         'mixed': 0 }
  2506.     tag = 'complexType'
  2507.  
  2508.  
  2509. class SimpleType(XMLSchemaComponent, DefinitionMarker, SimpleMarker):
  2510.     required = [
  2511.         'name']
  2512.     attributes = {
  2513.         'id': None,
  2514.         'name': None,
  2515.         'final': (lambda self: self._parent().getFinalDefault()) }
  2516.     contents = {
  2517.         'xsd': [
  2518.             'annotation',
  2519.             'restriction',
  2520.             'list',
  2521.             'union'] }
  2522.     tag = 'simpleType'
  2523.     
  2524.     def __init__(self, parent):
  2525.         XMLSchemaComponent.__init__(self, parent)
  2526.         self.annotation = None
  2527.         self.content = None
  2528.  
  2529.     
  2530.     def getElementDeclaration(self, attribute):
  2531.         raise Warning, 'invalid operation for <%s>' % self.tag
  2532.  
  2533.     
  2534.     def getTypeDefinition(self, attribute):
  2535.         raise Warning, 'invalid operation for <%s>' % self.tag
  2536.  
  2537.     
  2538.     def fromDom(self, node):
  2539.         self.setAttributes(node)
  2540.         contents = self.getContents(node)
  2541.         for child in contents:
  2542.             component = SplitQName(child.getTagName())[1]
  2543.             if component == 'annotation':
  2544.                 self.annotation = Annotation(self)
  2545.                 self.annotation.fromDom(child)
  2546.                 continue
  2547.             
  2548.         else:
  2549.             return None
  2550.         if component == 'restriction':
  2551.             self.content = self.__class__.Restriction(self)
  2552.         elif component == 'list':
  2553.             self.content = self.__class__.List(self)
  2554.         elif component == 'union':
  2555.             self.content = self.__class__.Union(self)
  2556.         else:
  2557.             raise SchemaError, 'Unknown component (%s)' % component
  2558.         self.content.fromDom(child)
  2559.  
  2560.     
  2561.     class Restriction(XMLSchemaComponent, RestrictionMarker):
  2562.         attributes = {
  2563.             'id': None,
  2564.             'base': None }
  2565.         contents = {
  2566.             'xsd': [
  2567.                 'annotation',
  2568.                 'simpleType'] + RestrictionMarker.facets }
  2569.         tag = 'restriction'
  2570.         
  2571.         def __init__(self, parent):
  2572.             XMLSchemaComponent.__init__(self, parent)
  2573.             self.annotation = None
  2574.             self.content = None
  2575.             self.facets = None
  2576.  
  2577.         
  2578.         def getAttributeBase(self):
  2579.             return XMLSchemaComponent.getAttribute(self, 'base')
  2580.  
  2581.         
  2582.         def getTypeDefinition(self, attribute = 'base'):
  2583.             return XMLSchemaComponent.getTypeDefinition(self, attribute)
  2584.  
  2585.         
  2586.         def getSimpleTypeContent(self):
  2587.             for el in self.content:
  2588.                 if el.isSimple():
  2589.                     return el
  2590.                     continue
  2591.             
  2592.  
  2593.         
  2594.         def fromDom(self, node):
  2595.             self.facets = []
  2596.             self.setAttributes(node)
  2597.             contents = self.getContents(node)
  2598.             content = []
  2599.             for indx in range(len(contents)):
  2600.                 component = SplitQName(contents[indx].getTagName())[1]
  2601.                 if component == 'annotation' and not indx:
  2602.                     self.annotation = Annotation(self)
  2603.                     self.annotation.fromDom(contents[indx])
  2604.                     continue
  2605.                     continue
  2606.                 if component == 'simpleType':
  2607.                     if not indx or indx == 1:
  2608.                         content.append(AnonymousSimpleType(self))
  2609.                         content[-1].fromDom(contents[indx])
  2610.                         continue
  2611.                 if component in RestrictionMarker.facets:
  2612.                     self.facets.append(contents[indx])
  2613.                     continue
  2614.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2615.             
  2616.             self.content = tuple(content)
  2617.  
  2618.  
  2619.     
  2620.     class Union(XMLSchemaComponent, UnionMarker):
  2621.         attributes = {
  2622.             'id': None,
  2623.             'memberTypes': None }
  2624.         contents = {
  2625.             'xsd': [
  2626.                 'annotation',
  2627.                 'simpleType'] }
  2628.         tag = 'union'
  2629.         
  2630.         def __init__(self, parent):
  2631.             XMLSchemaComponent.__init__(self, parent)
  2632.             self.annotation = None
  2633.             self.content = None
  2634.  
  2635.         
  2636.         def fromDom(self, node):
  2637.             self.setAttributes(node)
  2638.             contents = self.getContents(node)
  2639.             content = []
  2640.             for indx in range(len(contents)):
  2641.                 component = SplitQName(contents[indx].getTagName())[1]
  2642.                 if component == 'annotation' and not indx:
  2643.                     self.annotation = Annotation(self)
  2644.                     self.annotation.fromDom(contents[indx])
  2645.                     continue
  2646.                 if component == 'simpleType':
  2647.                     content.append(AnonymousSimpleType(self))
  2648.                     content[-1].fromDom(contents[indx])
  2649.                     continue
  2650.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2651.             
  2652.             self.content = tuple(content)
  2653.  
  2654.  
  2655.     
  2656.     class List(XMLSchemaComponent, ListMarker):
  2657.         attributes = {
  2658.             'id': None,
  2659.             'itemType': None }
  2660.         contents = {
  2661.             'xsd': [
  2662.                 'annotation',
  2663.                 'simpleType'] }
  2664.         tag = 'list'
  2665.         
  2666.         def __init__(self, parent):
  2667.             XMLSchemaComponent.__init__(self, parent)
  2668.             self.annotation = None
  2669.             self.content = None
  2670.  
  2671.         
  2672.         def getItemType(self):
  2673.             return self.attributes.get('itemType')
  2674.  
  2675.         
  2676.         def getTypeDefinition(self, attribute = 'itemType'):
  2677.             tp = XMLSchemaComponent.getTypeDefinition(self, attribute)
  2678.             if not tp:
  2679.                 pass
  2680.             return self.content
  2681.  
  2682.         
  2683.         def fromDom(self, node):
  2684.             self.annotation = None
  2685.             self.content = None
  2686.             self.setAttributes(node)
  2687.             contents = self.getContents(node)
  2688.             for indx in range(len(contents)):
  2689.                 component = SplitQName(contents[indx].getTagName())[1]
  2690.                 if component == 'annotation' and not indx:
  2691.                     self.annotation = Annotation(self)
  2692.                     self.annotation.fromDom(contents[indx])
  2693.                     continue
  2694.                 if component == 'simpleType':
  2695.                     self.content = AnonymousSimpleType(self)
  2696.                     self.content.fromDom(contents[indx])
  2697.                     break
  2698.                     continue
  2699.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2700.             
  2701.  
  2702.  
  2703.  
  2704.  
  2705. class AnonymousSimpleType(SimpleType, SimpleMarker, LocalMarker):
  2706.     required = []
  2707.     attributes = {
  2708.         'id': None }
  2709.     tag = 'simpleType'
  2710.  
  2711.  
  2712. class Redefine:
  2713.     tag = 'redefine'
  2714.  
  2715. if sys.version_info[:2] >= (2, 2):
  2716.     tupleClass = tuple
  2717. else:
  2718.     import UserTuple
  2719.     tupleClass = UserTuple.UserTuple
  2720.  
  2721. class TypeDescriptionComponent(tupleClass):
  2722.     
  2723.     def __init__(self, args):
  2724.         if len(args) != 2:
  2725.             raise TypeError, 'expecting tuple (namespace, name), got %s' % args
  2726.         elif args[1].find(':') >= 0:
  2727.             args = (args[0], SplitQName(args[1])[1])
  2728.         
  2729.         tuple.__init__(self, args)
  2730.  
  2731.     
  2732.     def getTargetNamespace(self):
  2733.         return self[0]
  2734.  
  2735.     
  2736.     def getName(self):
  2737.         return self[1]
  2738.  
  2739.  
  2740.